home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OCFSRC.PAK / EXCEPT.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  129 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.3  $
  6. //
  7. // OLE Exception classes
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #if !defined(OCF_DEFS_H)
  11. # include <ocf/defs.h>
  12. #endif
  13. #if !defined(OCF_OLEUTIL_H)
  14. # include <ocf/oleutil.h>
  15. #endif
  16. #if !defined(WINSYS_STRING_H)
  17. # include <winsys/string.h>
  18. #endif
  19. #if !defined(SERVICES_MEMORY_H)
  20. # include <services/memory.h>
  21. #endif
  22.  
  23.  
  24. // DLL which can provide a string for a give OLE error code
  25. //
  26. #if defined(BI_PLAT_WIN16)
  27. const char OleErrorStringsDLL[] = "OLE_ERR.DLL";
  28. #else
  29. const char OleErrorStringsDLL[] = "OLE_ERRF.DLL";
  30. #endif
  31.  
  32.  
  33. //
  34. //
  35. //
  36. TXOle::~TXOle()
  37. {
  38. }
  39.  
  40. //
  41. //
  42. //
  43. TXOle*
  44. TXOle::Clone()
  45. {
  46.   return new TXOle(*this);
  47. }
  48.  
  49. //
  50. //
  51. //
  52. void
  53. TXOle::Throw()
  54. {
  55.   THROW( *this );
  56. }
  57.  
  58. //
  59. //
  60. //
  61. void
  62. TXOle::Check(HRESULT hr, const char far* msg)
  63. {
  64.   if (FAILED(hr))
  65.     Throw(hr, msg);
  66. }
  67.  
  68. //
  69. //
  70. //
  71. void
  72. TXOle::Check(HRESULT hr)
  73. {
  74.   Check(hr, 0);
  75. }
  76.  
  77. //
  78. //
  79. //
  80. void
  81. TXOle::Throw(HRESULT hr, const char far* msg)
  82. {
  83.   if (!InstanceCount) {
  84.     char buf[128+1];
  85.     wsprintf(buf, "%s failed, ", msg ? msg : "OLE call");
  86.  
  87.     int len = strlen(buf);
  88.     OleErrorFromCode(hr, buf + len, sizeof(buf) - len - 2);
  89.     strcat(buf, ".");
  90.  
  91.     WARN(hr != HR_NOERROR, buf);
  92.     throw TXOle(buf, hr);
  93.   }
  94. }
  95.  
  96. //
  97. // If OLE-Error-String DLL exists, & msg string is there, then use
  98. // it. Otherwise just use the number.
  99. //
  100. void
  101. TXOle::OleErrorFromCode(HRESULT stat, char far* buffer, int size)
  102. {
  103.   const   char HResultToStringIDFunc[] = "StringIDFromHResult";
  104.   typedef int _export CALLBACK (*TStringIDFromHResult)(HRESULT hr);
  105.  
  106.   int len = 0;
  107.   OFSTRUCT ofs;
  108.   if (OpenFile(OleErrorStringsDLL, &ofs, OF_EXIST) != HFILE_ERROR) {
  109.     HINSTANCE hInst = LoadLibrary(OleErrorStringsDLL);
  110.     if (hInst > (HINSTANCE)HINSTANCE_ERROR) {
  111.       TStringIDFromHResult func =
  112.           (TStringIDFromHResult)GetProcAddress(hInst, HResultToStringIDFunc);
  113.       if (func) {
  114.         int id = func(stat);
  115.         if (id)
  116.           len = ::LoadString(hInst, id, buffer, size);
  117.       }
  118.       FreeLibrary(hInst);
  119.     }
  120.   }
  121.   if (!len) {
  122.     char numBuf[25];
  123.     wsprintf(numBuf, "ErrorCode: %8lX", stat);
  124.     strncat(buffer, numBuf, size - strlen(buffer) - 1);
  125.   }
  126.   buffer[size-1] = 0;
  127. }
  128.  
  129.